import JsBridge from '@/utils/JsBridge.js'
import { showToast, showLoadingToast, closeToast, contactListProps } from 'vant';
import { errorDeal } from './utils';

let isAndroid = navigator.userAgent.indexOf('Android') > -1 || navigator.userAgent.indexOf('Adr') > -1;
let isiOS = !!navigator.userAgent.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/);
let dev = !isAndroid

export default async function _http(apiName, params) {
    if (dev) return

    showLoadingToast({
        message: '请求中...',
        forbidClick: true,
        duration: 0,
    });

    return new Promise((resolve, reject) => {
        JsBridge.callHandler(apiName, params, (response) => {
            closeToast()
            let res = JSON.parse(response)

            if (res.code == 200) {
                let data = JSON.parse(res.msg)

                if (data.code == 200) {
                    resolve(data)
                } else {
                    errorDeal(data)
                    reject(data)
                }
            } else {
                errorDeal('基座接口调用失败！')
            }
        })
    })
}